<template>
  <div class="home-container">
    <el-container>
      <el-header> 
        <div>
          <img src="../assets/logo2.png" alt="">
          <span>后台管理系统</span>
        </div>
        <el-button type="info" @click="goLogin">退出</el-button>
      </el-header>
      <el-container :style="{height: containerHeight}">
        <!-- flag: true  64 -->
        <!-- flag: false  200 -->
        <el-aside :width="flag ? '64px' : '200px'">
          <div class="toggle-flag" @click="toggleFlag">|||</div>
          <el-menu
            :default-active="defaultActive"	
            router
            :collapse-transition="false"
            :collapse="flag"
            unique-opened
            background-color="#333744"
            text-color="#fff"
            active-text-color="#409EFF">
            <el-submenu :index="item.id + ''" v-for="item in menuList" :key="item.id">
              <template slot="title">
                <i class="mine-icon" :class="iconObj[item.id]"></i>
                <span>{{item.authName}}</span>
              </template>
              <el-menu-item @click="saveDefaultActive('/' + item2.path)" :index="'/' + item2.path" v-for="item2 in item.children" :key="item2.id">
                <i class="el-icon-menu"></i>
                <span>{{item2.authName}}</span>
              </el-menu-item>
            </el-submenu>
          </el-menu>
        </el-aside>
        <el-main>
          <router-view></router-view>
        </el-main>
      </el-container>
    </el-container>
  </div>
</template>
<script>
export default {
  data() {
    return {
      menuList: [],
      iconObj: {
        '125': 'iconfont icon-user',
        '101': 'iconfont icon-shangpin',
        '102': 'iconfont icon-danju',
        '103': 'iconfont icon-tijikongjian',
        '145': 'iconfont icon-baobiao'
      },
      flag: false,
      defaultActive: '',
      containerHeight: '565px'
    }
  },
  created() {
    this.getMenuList()
    this.defaultActive = sessionStorage.getItem('path') || ''
  },
  mounted() {
    this.containerHeight = window.innerHeight - 60 + 'px'
  },
  methods: {
    goLogin() {
      sessionStorage.clear()
      this.$router.push('/login')
      this.$message.success('退出成功')
    },
    async getMenuList() {
      const { data: res } = await this.$http.get('menus')

      if(res.meta.status !== 200) {
        this.$message.error(res.meta.msg)
        return false
      }

      this.menuList = res.data
    },
    toggleFlag() {
      this.flag = !this.flag
    },
    saveDefaultActive(path) {
      sessionStorage.setItem('path', path)
      this.defaultActive = path
    }
  }
}
</script>
<style lang="less" scoped>
  .home-container{
    height: 100%;
    .el-container{
      height: 100%;
    }
    .el-header{
      background: #373d41;
      padding-left: 0;
      display: flex;
      justify-content: space-between;
      align-items: center;
      color: #fff;
      div{
        display: flex;
        align-items: center;
        img{
          margin-right: 20px;
        }
        span{
          font-size: 20px;
          font-weight: 600;
        }
      }
    }
    .el-aside{
      background: rgb(51, 55, 68);
    }
    .el-main{
      background: #eaedf1;
    }
    .el-menu{
      border: 0;
    }
    .mine-icon{
      margin-right: 10px;
    }
    .toggle-flag{
      color: #fff;
      font-size: 12px;
      text-align: center;
      line-height: 25px;
      background-color: #4a5064;
      cursor: pointer;
      letter-spacing: .1em;
      user-select: none;
    }
  }
</style>